package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageBean;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DepartmentController {
    @Autowired
    private IDepartmentSerice departmentSerice;

    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){
                departmentSerice.save(department);
            }else {
                departmentSerice.update(department);
            }
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult(false,e.getMessage());
        }
    }
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long[] ids){
        try {
            for (long id: ids) {
                departmentSerice.delete(id);
            }
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult(false,e.getMessage());
        }
    }
    @PostMapping("/delList")
    public AjaxResult delList(@RequestBody List<Department> departments){
        try {
            for (Department de:departments) {
                departmentSerice.delete(de.getId());

            }
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult(false,e.getMessage());
        }
    }
    @GetMapping("/find")
    public List<Department> selectAll(){
        return departmentSerice.selectAll();
    }
    @GetMapping("/getManager")
    public List<Employee> getManager(){
        return departmentSerice.getManager();
    }

    @GetMapping("/{id}")
    public Department selectById(@PathVariable("id") Long id){
        return departmentSerice.selectById(id);
    }

    @PostMapping
    public PageBean<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        System.out.println(departmentQuery.getStates());

        return departmentSerice.queryPage(departmentQuery);
    }

    @PostMapping("/getMultistage")
    public List<Department> getDepartments(){

        return departmentSerice.getOneAndTwo();
    }


}
